<?php
/*
	$Id: admincp_article.php 12844 2012-08-21 gy $
*/

if(!defined('IN_UCHOME') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

//权限
if(!$allowmanage = checkperm('managearticle')) {
	cpmessage('no_authority_management_operation');
}


if(!empty($_POST['op'])&&$_POST['op']=='add'){
	$data['uid'] = $_SGLOBAL['supe_uid'];
	$data['title'] = trim($_POST['title']);
	$data['stitle'] = trim($_POST['stitle']);
	$data['keywords'] = trim($_POST['keywords']);
	$data['typeid'] = intval($_POST['typeid']);
	$data['description'] = trim($_POST['description']);
	$data['writer'] = trim($_POST['writer']);
	$data['source'] = trim($_POST['source']);
	$data['click'] = intval($_POST['click']);
	$data['pic'] = trim($_POST['pic']);
	$data['body'] = trim($_POST['body']);
	$data['atime'] = time();
	$data['arcrank'] = 0;
	$insert = inserttable('article',$data,1);
	if($insert){
		cpmessage('article_success', 'admincp.php?ac=article');
	}else{
		cpmessage('article_fail', 'admincp.php?ac=article');
	}
}

if(!empty($_POST['op'])&&$_POST['op']=='edit'){
	$data['title'] = trim($_POST['title']);
	$data['stitle'] = trim($_POST['stitle']);
	$data['keywords'] = trim($_POST['keywords']);
	$data['typeid'] = intval($_POST['typeid']);
	$data['description'] = trim($_POST['description']);
	$data['writer'] = trim($_POST['writer']);
	$data['source'] = trim($_POST['source']);
	$data['click'] = intval($_POST['click']);
	$data['pic'] = trim($_POST['pic']);
	$data['body'] = trim($_POST['body']);
	$data['atime'] = time();
	$data['arcrank'] = 0;
	$aid = intval($_POST['aid']);
	updatetable('article', $data, array('aid'=>$aid));
	cpmessage('do_success', 'admincp.php?ac=article');
}

if($_GET['op'] == 'add') {	
	if(!$allowmanage){cpmessage('no_authority_management_operation');}
	$actives = array('add' => 'class="active"');
	$op = $_GET['op'];
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('arctype')." WHERE reid = 0 ");
	while($value = $_SGLOBAL['db']->fetch_array($query)) {
		$type .="<option value='".$value['lid']."'>".$value['typename']."</option>";
		$seid = $value['lid'];
		$sp = '-';
		$select = '';
		Datas($seid,$sp,$select);
	}
}elseif($_GET['op'] == 'check'){
	if(!$allowmanage){cpmessage('no_authority_management_operation');}
	$aid = intval($_GET['aid']);
	$arcrank = 1;
	updatetable('article', array('arcrank'=>$arcrank), array('aid'=>$aid));
	cpmessage('do_success', 'admincp.php?ac=article');
}elseif($_GET['op'] == 'edit') {	
	if(!$allowmanage){cpmessage('no_authority_management_operation');}
	$actives = array('edit' => 'class="active"');
	$aid = intval($_GET['aid']);
	$op = $_GET['op'];
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('article')." WHERE aid =".$aid);
	$art = $_SGLOBAL['db']->fetch_array($query);
	$select = $art['typeid'];

	$editsql = $_SGLOBAL['db']->query("SELECT * FROM ".tname('arctype')." WHERE reid = 0 ");
	while($value = $_SGLOBAL['db']->fetch_array($editsql)){
		$seid = $value['lid'];
		if($seid == $select){
			$type .="<option value='".$value['lid']."' selected='selected'>".$value['typename']."</option>";
		}else{
			$type .="<option value='".$value['lid']."'>".$value['typename']."</option>";
		}	
		$sp = '-';
		Datas($seid,$sp,$select);
	}
}elseif($_GET['op'] == 'del') {	
	if(!$allowmanage){cpmessage('no_authority_management_operation');}
	$aid = intval($_GET['aid']);
	$_SGLOBAL['db']->query("DELETE FROM ".tname('article')." WHERE aid=".$aid);
	cpmessage('do_success', 'admincp.php?ac=article');
}else{
	$mpurl = 'admincp.php?ac=article';

	//处理搜索
	$intkeys = array('uid', 'uid');
	$strkeys = array();
	$randkeys = array(array('sstrtotime','atime'));
	$likekeys = array();
	$results = getwheres($intkeys, $strkeys, $randkeys, $likekeys);
	$wherearr = $results['wherearr'];
	$wheresql = empty($wherearr)?'1':implode(' AND ', $wherearr);
	$mpurl .= '&'.implode('&', $results['urls']);
	
	//排序
	$orders = getorders(array('atime'), 'aid');
	$ordersql = $orders['sql'];
	if($orders['urls']) $mpurl .= '&'.implode('&', $orders['urls']);
	$orderby = array($_GET['orderby']=>' selected');
	$ordersc = array($_GET['ordersc']=>' selected');
	
	//激活
	if(empty($_GET['aid'])) {
		$actives = array('all' => 'class="active"');
	}
	
	$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
	if(!in_array($perpage, array(20,50,100))) $perpage = 20;
	
	$page = empty($_GET['page'])?1:intval($_GET['page']);
	if($page<1) $page = 1;
	$start = ($page-1)*$perpage;
	//检查开始数
	ckstart($start, $perpage);

	$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('article')." WHERE $wheresql"), 0);
	$selectsql = 'a.*,t.*';

	$mpurl .= '&perpage='.$perpage;
	$perpages = array($perpage => ' selected');
	
	$list = array();
	$multi = '';
	$managebatch = checkperm('admin');
	$allowbatch = true;
	if($count) {
		
		$query = $_SGLOBAL['db']->query("SELECT $selectsql FROM ".tname('article')." AS a LEFT JOIN ".tname('arctype')." AS t ON a.typeid = t.lid WHERE $wheresql $ordersql LIMIT $start,$perpage");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$list[] = $value;
		}
		$multi = multi($count, $perpage, $page, $mpurl);
	}
}

function Datas($seid=0,$sp,$select){
	global $type,$_SGLOBAL;
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('arctype')." WHERE reid =".$seid." ORDER BY tptime");
	while($value = $_SGLOBAL['db']->fetch_array($query)) {
		$seid = $value['lid'];
		if($seid == $select){
			$type .="<option value='".$value['lid']."' selected='selected'>".$sp.$value['typename']."</option>";
		}else{
			$type .="<option value='".$value['lid']."'>".$sp.$value['typename']."</option>";
		}
		Datas($seid,$sp.'-',$select);
	}
}
?>